package com.xtt._04_Apache.commons_io;

import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.List;

import org.apache.commons.io.*;

/**
 *   这个工具类包分如下几个主要工具类：
 1） FilenameUtils:主要处理各种操作系统下对文件名的操作
 2) FileUtils:处理文件的打开，移动，读取和判断文件是否存在
 3） IOCASE：字符串的比较
 4） FileSystemUtils:返回磁盘的空间大小
 */
public final class UtilityExample {
    
    // We are using the file exampleTxt.txt.txt in the folder ExampleFolder,
    // and we need to provide the full path to the Utility classes.
    private static final String EXAMPLE_TXT_PATH =
            "src\\main\\java\\com\\xtt\\_04_Apache\\commons_io\\ExampleFolder\\exampleTxt.txt";
    private static final String EXAMPLE_TXT_PATH_COPY =
            "src\\main\\java\\com\\xtt\\_04_Apache\\commons_io\\ExampleFolder\\exampleTxtCopy.txt";
    private static final String EXAMPLE_TXT_PATH_COPY2=
            "src\\main\\java\\com\\xtt\\_04_Apache\\commons_io\\ExampleFolder\\exampleTxtCopy2.txt";
    private static final String PARENT_DIR =
            "src\\main\\java\\com\\xtt\\_04_Apache\\commons_io";

    public static void runExample() throws IOException {
        System.out.println("Utility Classes example...");


        /**
         *FilenameUtils
         */
        //FilenameUtils.getFullPath 获取全路径
        System.out.println("Full path of exampleTxt.txt: " +
                FilenameUtils.getFullPath(EXAMPLE_TXT_PATH));
        //src\main\java\com\xtt\_04_Apache\commons_io\ExampleFolder\

        //FilenameUtils.getName 获取文件名 含扩展
        System.out.println("Full name of exampleTxt.txt: " +
                FilenameUtils.getName(EXAMPLE_TXT_PATH));
        //exampleTxt.txt.txt

        //FilenameUtils.getExtension 获取文件扩展名
        System.out.println("Extension of exampleTxt.txt: " +
                FilenameUtils.getExtension(EXAMPLE_TXT_PATH));
        //txt

        //FilenameUtils.getBaseName 获取文件名 不含扩展
        System.out.println("Base name of exampleTxt.txt: " +
                FilenameUtils.getBaseName(EXAMPLE_TXT_PATH));
        //exampleTxt.txt

        /**
         * FileUtils
         */
        // We can create a new File object using FileUtils.getFile(String)
        // and then use this object to get information from the file.
        //获取File对象
        File exampleFile = FileUtils.getFile(EXAMPLE_TXT_PATH);
        //获取行遍历 LineInterator 将BufferedReader封装成遍历器，BufferedReader.readLine实现
        LineIterator iter = FileUtils.lineIterator(exampleFile);
        
        System.out.println("Contents of exampleTxt.txt...");
        //按行遍历输入文件内容
        while (iter.hasNext()) {
            System.out.println("\t" + iter.next());
        }
        iter.close();
        
        // We can check if a file exists somewhere inside a certain directory.
        //一个目录向是否包含某文件 (getCanonicalPath() 获取文件绝对路径，比较两路径是否相等或以此开头)
        File parent = FileUtils.getFile(PARENT_DIR);
        System.out.println("Parent directory contains exampleTxt.txt file: " +
                FileUtils.directoryContains(parent, exampleFile));
        //true
        
        // IOCase
        
        String str1 = "This is a new String.";
        String str2 = "This is another new String, yes!";
        //checkEndsWith 大小写敏感 （使用regionMatches实现）
        System.out.println("Ends with string (case sensitive): " +
                IOCase.SENSITIVE.checkEndsWith(str1, "string."));//false
        //checkEndsWith 大小写不敏感
        System.out.println("Ends with string (case insensitive): " +
                IOCase.INSENSITIVE.checkEndsWith(str1, "string."));//true
        //checkEquals
        System.out.println("String equality: " +
                IOCase.SENSITIVE.checkEquals(str1, str2));
        
        
        // FileSystemUtils
        // 获取剩余可用空间
        System.out.println("Free disk space (in KB): " + FileSystemUtils.freeSpaceKb("C:"));
        System.out.println("Free disk space (in MB): " + FileSystemUtils.freeSpaceKb("C:") / 1024);
        System.out.println("Free disk space (in GB): " + FileSystemUtils.freeSpaceKb("C:") / 1024/1024);

        //IOUtils
        FileUtils.getFile(EXAMPLE_TXT_PATH_COPY).delete();
        FileUtils.getFile(EXAMPLE_TXT_PATH_COPY2).delete();
        List<String> strings = IOUtils.readLines(new FileInputStream(EXAMPLE_TXT_PATH), StandardCharsets.UTF_8);
        strings.stream().forEach(System.out::println);
        IOUtils.writeLines(strings,null,new FileOutputStream(EXAMPLE_TXT_PATH_COPY),StandardCharsets.UTF_8);

        BufferedInputStream buffer = IOUtils.buffer(new FileInputStream(EXAMPLE_TXT_PATH));
        BufferedOutputStream buffer1 = IOUtils.buffer(new FileOutputStream(EXAMPLE_TXT_PATH_COPY));
        BufferedReader buffer2 = IOUtils.buffer(new FileReader(EXAMPLE_TXT_PATH));
        BufferedWriter buffer3 = IOUtils.buffer(new FileWriter(EXAMPLE_TXT_PATH_COPY));
        IOUtils.closeQuietly(buffer);
        IOUtils.closeQuietly(buffer1);
        IOUtils.closeQuietly(buffer2);
        IOUtils.closeQuietly(buffer3);


        IOUtils.contentEquals(new FileInputStream(EXAMPLE_TXT_PATH),new FileInputStream(EXAMPLE_TXT_PATH_COPY));
        InputStream in = new FileInputStream(EXAMPLE_TXT_PATH);
        OutputStream out = new FileOutputStream(EXAMPLE_TXT_PATH_COPY2);
        IOUtils.copy(in,out);
        IOUtils.closeQuietly(in);
        IOUtils.closeQuietly(out);
    }
}